package com.yww.leetcode2.array;

/**
 * @author yww
 * @description 977. 有序数组的平方
 * @since 2024/10/8
 */
public class SortedSquares977_03 {
    // 快慢指针解法
    public int[] sortedSquares(int[] nums) {
        int[] result = new int[nums.length];
        int k = nums.length - 1;
        int i = 0, j = nums.length - 1;
        while (i <= j) {
            if (nums[i] * nums[i] > nums[j] * nums[j]) {
                result[k--] = nums[i] * nums[i];
                i++;
            } else {
                result[k--] = nums[j] * nums[j];
                j--;
            }
        }
        return result;
    }
}
